package com.nealin.shardingjdbc.controller;

import com.nealin.shardingjdbc.entity.Dictionary;
import com.nealin.shardingjdbc.entity.Order;
import com.nealin.shardingjdbc.entity.OrderItem;
import com.nealin.shardingjdbc.mapper.OrderItemMapper;
import com.nealin.shardingjdbc.mapper.OrderMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 订单表(Order)表控制层
 *
 * @author HuangBingzhi
 * @since 2020-04-12 23:00:56
 */
@RestController
@RequestMapping("order")
public class OrderController {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderItemMapper itemMapper;

    @GetMapping("insert")
    public int insert(Long id) {
        Order order=new Order();
        order.setOrderName("电视机")
                .setCreateTime(new Date())
                .setCreateUser(2L);
        orderMapper.insertNoId(order);
        return 1;
    }

    @GetMapping("update")
    public int update(Long id) {
        Order order=new Order();
        order.setOrderId(id)
                .setOrderName("电视机1")
                .setCreateTime(new Date());
        return orderMapper.update(order);
    }

    @GetMapping("getById")
    public Order getById(Long id) {
        return orderMapper.queryById(id);
    }

    @PostMapping("getByName")
    public List<Order> getByName(@RequestBody Order order) {
        return orderMapper.queryAll(order);
    }

    @GetMapping("insertOrderAndItem")
    public void insertOrderAndItem() {
        for (int i=0; i < 10; i++) {
            Order order=new Order();
            order.setOrderName("电视机" + i)
                    .setCreateTime(new Date())
                    .setCreateUser((long) i)
                    .setOrderType(String.valueOf(i%3));
            orderMapper.insertNoId(order);
            OrderItem item=new OrderItem();
            item.setOrderId(order.getOrderId());
            item.setItemName("电源" + i);
            item.setCreateUser((long) i);
            item.setCreateTime(new Date());
            itemMapper.insert(item);
        }

    }

    @PostMapping("getOrderAndItem")
    public List<Order> getOrderAndItem(@RequestBody Order order) {
        List<Order> orders=orderMapper.getOrderAndItem(order);
        return orders;
    }

    @PostMapping("insertDictionary")
    public Integer getOrderAndItem(@RequestBody Dictionary dictionary) {
        final int count=orderMapper.insertDictionary(dictionary);
        return count;
    }
    @GetMapping("getDictionary")
    public Dictionary getDictionary(Long id) {
        Dictionary count=orderMapper.getDictionaryById(id);
        return count;
    }
}